package com.education.springbootdemo.controller;

import com.auth0.jwt.interfaces.Claim;
import com.education.springbootdemo.domain.Resp;
import com.education.springbootdemo.mail.EmailService;
import com.education.springbootdemo.mail.EmailServiceImpl;
import com.education.springbootdemo.service.UserService;
import com.education.springbootdemo.utils.SessionUtils;
import com.education.springbootdemo.utils.TokenUtils;
import com.education.springbootdemo.vo.User;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Pattern;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Validated
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private EmailService emailService;

    /*登录用户信息*/
    @GetMapping("/login_info")
    public User getUserByToken() {
        Long id = SessionUtils.getUserId();
        User user = userService.selectById(id);

        //角色
        //权限
        //分组
        return user;
    }

    // path  /user/{id}
    // RESTFul的规范，  /book/1  /book/2

    @GetMapping("/{id}")
    public User getUserById(@PathVariable Long id) {
        return userService.selectById(id);
    }

    @PostMapping("")
    public Long insertUser(@RequestBody User user) {
        return userService.insertUser(user);
    }

    @GetMapping("/kkk")
    public String getString() {
        // 接口需要登录用户ID
        // 使用拦截器提取用户ID

        return "hkkkkk";
    }

    @GetMapping("/send_mail")
    public boolean sendMail(String to, String subject, String content) {
        //直接使用Service的构造示例，两个地方是不行
        // 1、就是Service实例依赖的属性，无法注入
        // 2、每次请求都会创建实例，导致java对象持续增多，会出现GC的情况。影响性能。
//        EmailServiceImpl impl = new EmailServiceImpl();

        to = "252962599@qq.com";
        subject = "发送邮件开发学习";
        content = "Springboot集成邮件";
        boolean b = emailService.sendEmail(to,subject,content);
        return b;
    }

    @PostMapping("/reg")
    public Resp<User> reg(@RequestBody User user) {
       User ret =  userService.reg(user.getUsername(),user.getPassword());

       if (ret != null) {
           return Resp.success(ret);
       }

       return Resp.error(500,"注册失败");
    }
}
